package com.qfedu.hotelssm.service.impl;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.read.listener.ReadListener;
import com.qfedu.hotelssm.dao.PassengersDao;
import com.qfedu.hotelssm.dao.RoomsDao;
import com.qfedu.hotelssm.dto.PassengersExportDto;
import com.qfedu.hotelssm.entity.Passengers;
import com.qfedu.hotelssm.service.intf.PassengersService;
import com.qfedu.hotelssm.util.StringUtil;
import com.qfedu.hotelssm.vo.PageBean;
import com.qfedu.hotelssm.vo.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;

/**
 * @author mk
 * @date 2022/8/28  21:03
 */
@Service
public class PassengersServiceImpl implements PassengersService {
    @Autowired
    private PassengersDao dao;

    @Autowired
    private RoomsDao roomsDao;

    @Override
    public R save(Passengers passengers) {
        int save = dao.save(passengers);
        if (save > 0){
            //修改房间状态
            int updateRoom = roomsDao.updateRoomStatus(passengers.getRid(),1,passengers.getId());
            if (updateRoom > 0){
                return R.ok();
            }
        }
        return R.fail();
    }

    @Override
    public R update(Passengers passengers) {
        if(dao.update(passengers)>0){
            return R.ok();
        }else {
            return R.fail();
        }
    }

    @Override
    public R delete(int id) {
        //获取旅客信息
        Passengers personInfo = dao.selectOne(id);
        //修改房间状态为未入住
        int update = roomsDao.updateRoomStatus(personInfo.getRid(),0,null);
        //删除旅客信息
        if (update >0){
            if(dao.delete(id)>0){
                return R.ok();
            }
        }
        return R.fail();
    }

    @Override
    public PageBean selectPage(String name, int page, int limit) {
        if(StringUtil.isNotEmpty(name)){
            name="%"+name+"%";
        }
        int start=(page-1)*limit;

        PageBean pageBean = new PageBean((int) dao.selectCount(name), dao.selectPage(name, start, limit));

        return pageBean;
    }

    @Override
    public R delIds(int[] ids) {
        if(dao.delByIds(ids)>0){
            return R.ok();
        }else {
            return R.fail();
        }
    }

    @Override
    public List<PassengersExportDto> all() {
        return dao.all();
    }

    @Override
    public R upload(MultipartFile file) throws IOException {
        if(!file.isEmpty()){
            List<Passengers> list= EasyExcel.read(file.getInputStream(), Passengers.class, new ReadListener() {
                @Override
                public void invoke(Object o, AnalysisContext analysisContext) {
                }
                @Override
                public void doAfterAllAnalysed(AnalysisContext analysisContext) {
                }
            }).sheet().doReadSync();
            if(dao.saveBatch(list)>0 ){
                return  R.ok();
            }
        }
        return R.fail();
    }


}
